<?php  
class ControllerModulecategoryfilter extends Controller {
	protected function index($setting) {
		
		$filter_category = array();
		
		if(isset($this->request->get['filter']))
		{
			$filter_category = explode(',', $this->request->get['filter']);
		}
		
		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}
		
		if (isset($parts[0])) {
			$this->data['category_id'] = $parts[0];
			//$filter_category[] = $parts[0];
		} else {
			$this->data['category_id'] = 0;
		}
		
		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
			$filter_category[] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}
		
		
			
			if (isset($this->request->get['filter'])) {
				$this->data['filter_category'] = explode(',', $this->request->get['filter']);
				$this->data['child_id'] = 1000;
			} else {
				$this->data['filter_category'] = array();
			}
			
			
		if (isset($this->request->get['filter'])) {
				$this->data['filter_category'] = explode(',', $this->request->get['filter']);
			} else {
				$this->data['filter_category'] = array();
			}
			
		$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}	
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
			
		
			
			
		$this->data['action'] = str_replace('&amp;', '&', $this->url->link('product/category',  $url));
		
		
		$this->load->model('catalog/category');

		$this->load->model('catalog/product');

		$this->data['categories'] = array();

		//$categories = $this->model_catalog_category->getAllCategories();
		array_unshift($this->data['filter_category'],0);
		$in = false;
		
		$categories = $this->model_catalog_category->getCategories(0);

		foreach ($categories as $category) {
		if ($category['top']) {	
				
				$category_info = $this->model_catalog_category->getCategory($category['category_id']);
				
				$total = $this->model_catalog_product->getTotalProducts(array('filter_category_id' => $category['category_id']));
				
	
				$children_data = array();
				$children_select = array();
	
				$children = $this->model_catalog_category->getCategories($category['category_id']);
				
				
				//if($this->data['category_id'] == $category['category_id'] ) $in = true;
				
				
				foreach ($children as $child) {
					
							$fcat= $filter_category;
							
							$fcat[] = $child['category_id'];
					
					
					
					//print_r($filter_category);
							/*$data = array(
								'filter_category_id'  => $child['category_id'],
								'category_filter' => $filter_category,
								'filter_sub_category' => true
							);*/
							$data = array(
								
								'category_filter' => $fcat
							);
							if($this->data['child_id'] == $child['category_id'] ) $in = true;
							
							$product_total = $this->model_catalog_product->getTotalProductsFilter($data);
			
							$total += $product_total;
							//Ако няма продукти да не се показва
							if($product_total){
								
								if($child['category_id'] == $this->data['child_id'] ) 
								{
									$in = true;
									$this->data['filter_category'][] = $child['category_id'];
								}
				
								if( array_search($child['category_id'],$this->data['filter_category']) || $in )
								{
									$children_select[] = array(
									'category_id' => $child['category_id'],
									'name'        => $child['name'] .  ' (' . $product_total . ')' ,
									'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
									);
								
								}
								
								
								$children_data[] = array(
									'category_id' => $child['category_id'],
									'name'        => $child['name'] .   ' (' . $product_total . ')' ,
									'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
								);
								
								
								if($children_select) $children_data = $children_select;
								$in = false;
						}
					
				}
	
				$this->data['categories'][] = array(
					'category_id' => $category['category_id'],
					'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
					'children'    => $children_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
				);	
			}
		}
		
		
		
		
		
		
		$this->load->model('catalog/information');
		
		$this->data['informations'] = array();

		foreach ($this->model_catalog_information->getInformations() as $result) {
			if ($result['top']) {
				$this->data['informations'][] = array(
					'title' => $result['title'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);	
			}
    	}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/categoryfilter.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/categoryfilter.tpl';
		} else {
			$this->template = 'default/template/module/categoryfilter.tpl';
		}
		
		$this->render();
  	}
}
?>